exports.deleteApplication = function(req,res){

    var bodyParams = req.routeInfo.bodyParams;
    
    //容器数据
    var containerData = [];

    //需要删除容器数量
    var containerLength = 0;

    //已删除完成的容器服务数量
    var deleteSuccessContainerLength = 0;

    //已删除完成的容器记录数量
    var deleteSuccessContainerRecordLength = 0;

    //判断是否有未删除的模板
    function isApplicationTemplate(cb){

        R.SEND_HTTP(
            req,
            res,
            {
                url    : CORE_APIConfig.coreAPI.easydCloudMemberApplicationTemplateSearch.Request_Api_Url + "/cmat_cmaid/" + bodyParams.cma_id + "/cmat_is_delete/0",  
                method : 'POST',
                data   : {
                    fields : "cmat_id"
                }
            },
            function (data) {

                if(data.result.data.length > 0){
                   return RES.errorResponse(res,"抱歉，该应用下还有未删除的模板，请先删除模板");  
                }

                cb();

            }
        )
        

    }

    //删除应用 
    function deletUserApplication(cb){

        R.SEND_HTTP(
            req,
            res,
            {
                url    : CORE_APIConfig.coreAPI.easydCloudMemberApplicationDelete.Request_Api_Url,  
                method : 'POST',
                data   : {
                    delete_where  : 'cma_id',
                    cma_id        : bodyParams.cma_id,
                    cma_is_delete : 1
                }
            },
            function (data) {

                cb();

            }
        )
    }


    //查询应用相关容器
    function searchApplicationContainer(cb){

        R.SEND_HTTP(
            req,
            res,
            {
                url    : CORE_APIConfig.coreAPI.easydCloudMemberApplicationContainerSearch.Request_Api_Url + "/cmac_cmaid/" + bodyParams.cma_id + "/cmac_is_delete/0",  
                method : 'POST',
                data   : {
                    fields : 'cmac_id,cmac_name'
                }
            },
            function (data) {

                containerData = data.result.data;
             
                cb();

            }
        )
    }
    

    //停止并删除相关容器服务
    function stopAndRemoveContainer(cb){

        //需要删除容器数量
        containerLength = containerData.length;

        //循环容器并进行删除操作
        F.SerialEach(containerData,function(item,index,callback){

            R.Ssh_exec_dockerCommand(`${DockerSH.RemoveAppContainer} ${item.cmac_name}`,function(rs){

                //判断是否有错误提示
                if(rs.indexOf('Error') != -1){
                    return RES.errorResponse(res,"应用容器删除失败，请重新尝试");
                }else{
                    deleteSuccessContainerLength++;
                    callback();
                }
                
            })


        },cb)

    }

    //验证容器是否全部删除完成
    function isDeleteContainerComplate(cb){

        if(containerLength != deleteSuccessContainerLength){
            return RES.errorResponse(res,"应用云服务容器未删除完全，请重新检查");
        }

        cb();

    }

    //删除容器记录
    function removeContainerRecord(cb){

        //循环删除容器记录
        F.SerialEach(containerData,function(item,index,callback){

            R.SEND_HTTP(
                req,
                res,
                {
                    url    : CORE_APIConfig.coreAPI.easydCloudMemberApplicationContainerDelete.Request_Api_Url,  
                    method : 'POST',
                    data   : {
                        delete_where   : 'cmac_id',
                        cmac_id        : item.cmac_id,
                        cmac_is_delete : 1
                    }
                },
                function (data) {

                    deleteSuccessContainerRecordLength++;
    
                    callback();
    
                }
            )

        },cb)


    }

    //验证容器记录是否全部删除完成
    function isRemoveContainerRecord(cb){

        if(containerLength != deleteSuccessContainerRecordLength){
            return RES.errorResponse(res,"应用云服务容器记录未删除完全，请重新检查");
        }

        cb();

    }

    //删除用户应用项目
    function deleteUserApplicationProject(cb){

        R.Ssh_exec_dockerCommand(`${DockerSH.RemoveAppUserApplicationProject} ${bodyParams.username} ${bodyParams.appKey}`,function(rs){

            //判断是否有错误提示
            if(rs.indexOf('Error') != -1){
                return RES.errorResponse(res,"用户应用项目删除失败，请重新尝试");
            }else{
                cb();
            }
            
        })

    }

    F.Serial([
        isApplicationTemplate,
        deletUserApplication,
        searchApplicationContainer,
        stopAndRemoveContainer,
        isDeleteContainerComplate,
        removeContainerRecord,
        isRemoveContainerRecord,
        deleteUserApplicationProject
    ],() => {
        RES.successResponse(res);
    })

}